﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Text;
using System.Windows.Forms;

namespace PictureSplit
{
    // Token: 0x0200000C RID: 12
    public partial class Split_Form : Form
    {
        // Token: 0x17000005 RID: 5
        // (set) Token: 0x0600003F RID: 63 RVA: 0x00004849 File Offset: 0x00003849
        public bool IsCreateHtml
        {
            set
            {
                this.isCreateHtml = value;
            }
        }

        // Token: 0x17000006 RID: 6
        // (set) Token: 0x06000040 RID: 64 RVA: 0x00004854 File Offset: 0x00003854
        public string SaveImageFormat
        {
            set
            {
                if (value != null)
                {
                    if (value == "bmp")
                    {
                        this.imageFormat = ImageFormat.Bmp;
                        this.saveFileFormat = ".bmp";
                        return;
                    }
                    if (value == "jpg")
                    {
                        this.imageFormat = ImageFormat.Jpeg;
                        this.saveFileFormat = ".jpg";
                        return;
                    }
                    if (value == "gif")
                    {
                        this.imageFormat = ImageFormat.Gif;
                        this.saveFileFormat = ".gif";
                        return;
                    }
                    if (value == "png")
                    {
                        this.imageFormat = ImageFormat.Png;
                        this.saveFileFormat = ".png";
                        return;
                    }
                    if (value == "tiff")
                    {
                        this.imageFormat = ImageFormat.Tiff;
                        this.saveFileFormat = ".tiff";
                        return;
                    }
                }
                this.imageFormat = ImageFormat.Jpeg;
                this.saveFileFormat = ".jpg";
            }
        }

        // Token: 0x17000007 RID: 7
        // (set) Token: 0x06000041 RID: 65 RVA: 0x0000493D File Offset: 0x0000393D
        public string SourceFileName
        {
            set
            {
                this.sourceFileName = value;
            }
        }

        // Token: 0x17000008 RID: 8
        // (set) Token: 0x06000042 RID: 66 RVA: 0x00004947 File Offset: 0x00003947
        public string SaveFileName
        {
            set
            {
                this.saveFileName = value;
            }
        }

        // Token: 0x17000009 RID: 9
        // (set) Token: 0x06000043 RID: 67 RVA: 0x00004951 File Offset: 0x00003951
        public int WidthNumber
        {
            set
            {
                this.widthNumber = value;
            }
        }

        // Token: 0x1700000A RID: 10
        // (set) Token: 0x06000044 RID: 68 RVA: 0x0000495B File Offset: 0x0000395B
        public int HeightNumber
        {
            set
            {
                this.heightNumber = value;
            }
        }

        public int Quality
        {
            set
            {
                if (value >= 1 && value <= 100)
                {
                    this.quality = value;
                }
            }
        }

        // Token: 0x1700000B RID: 11
        // (set) Token: 0x06000045 RID: 69 RVA: 0x00004965 File Offset: 0x00003965
        public int WidthPixel
        {
            set
            {
                this.widthPixel = value;
            }
        }

        // Token: 0x1700000C RID: 12
        // (set) Token: 0x06000046 RID: 70 RVA: 0x0000496F File Offset: 0x0000396F
        public int HeightPixel
        {
            set
            {
                this.heightPixel = value;
            }
        }
        public Bitmap ImageSrc
        {
            set
            {
                this.m_imageSrc = value;
            }
        }

        // Token: 0x06000047 RID: 71 RVA: 0x00004979 File Offset: 0x00003979
        public Split_Form()
        {
            this.InitializeComponent();
        }

        // Token: 0x06000048 RID: 72 RVA: 0x000049B8 File Offset: 0x000039B8
        private void btnOK_Click(object sender, EventArgs e)
        {
            if (this.isCreateHtml && this.chkOpenHTML.Checked)
            {
                new PreviewForm
                {
                    HtmlFileName = this.saveFileName + ".htm"
                }.ShowDialog();
            }
            base.Close();
        }

        // Token: 0x06000049 RID: 73 RVA: 0x00004A10 File Offset: 0x00003A10
        private void CreateHTML(string path, int row, int col, string pictureName, string expandName, bool isDisplaySplitGrid)
        {
            string value = "<html>\r\n<head>\r\n<meta http-equiv=\"Content-Type\" content=\"text/html; charset=gb2312\">\r\n<title>CQ图片分割器生成网页</title>\r\n</head>\r\n";
            StreamWriter streamWriter = new StreamWriter(path + ".Htm", false, Encoding.UTF8);
            streamWriter.Write(value);
            streamWriter.WriteLine("<body>\r\n");
            if (isDisplaySplitGrid)
            {
                streamWriter.WriteLine("<table border=\"1\" id=\"table1\" cellspacing=\"0\" cellpadding=\"0\" bordercolor=\"#FF0000\">");
            }
            else
            {
                streamWriter.WriteLine("<table border=\"0\" id=\"table1\" cellspacing=\"0\" cellpadding=\"0\">");
            }
            for (int i = 0; i < row; i++)
            {
                streamWriter.WriteLine("<tr>");
                for (int j = 0; j < col; j++)
                {
                    streamWriter.WriteLine(string.Concat(new string[]
                    {
                        "<td><img border=\"0\" src=\"",
                        pictureName,
                        "_",
                        i.ToString("00"),
                        "x",
                        j.ToString("00"),
                        expandName,
                        "\"></td>"
                    }));
                }
                streamWriter.WriteLine("</tr>");
            }
            streamWriter.WriteLine("</table>\r\n");
            streamWriter.WriteLine("</body>");
            streamWriter.WriteLine("</html>");
            streamWriter.Close();
        }
        public static Bitmap CutImage(Image b, int StartX, int StartY, int iWidth, int iHeight)
        {
            if (b == null)
            {
                return null;
            }
            int iDstWidth = iWidth;
            int iDstHeight = iHeight;
            int w = b.Width;
            int h = b.Height;
            if (StartX >= w || StartY >= h)
            {
                // 开始截取坐标过大时，结束处理 
                return null;
            }
            if (StartX + iWidth > w)
            {
                // 宽度过大时只截取到最大大小 
                iWidth = w - StartX;
            }
            if (StartY + iHeight > h)
            {
                // 高度过大时只截取到最大大小 
                iHeight = h - StartY;
            }
            try
            {
                Bitmap bmpOut = new Bitmap(iDstWidth, iDstHeight);
                Graphics g = Graphics.FromImage(bmpOut);
                g.DrawImage(b, new Rectangle(0, 0, iWidth, iHeight), new Rectangle(StartX, StartY, iWidth, iHeight), GraphicsUnit.Pixel);
                g.Dispose();
                return bmpOut;
            }
            catch
            {
                return null;
            }
        }
        // Token: 0x0600004A RID: 74
        private void SplitPicture()
        {
            string path = Path.GetDirectoryName(this.saveFileName) + "\\";

            //以下代码为保存图片时，设置压缩质量 
            EncoderParameters ep = new EncoderParameters();
            long[] qy = new long[1];
            qy[0] = quality;//设置压缩的比例1-100 
            EncoderParameter eParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, qy);
            ep.Param[0] = eParam;

            ImageCodecInfo[] arrayICI = ImageCodecInfo.GetImageEncoders();
            ImageCodecInfo jpegICIinfo = null;
            for (int x = 0; x < arrayICI.Length; x++)
            {
                if (arrayICI[x].FormatDescription.Equals("JPEG"))
                {
                    jpegICIinfo = arrayICI[x];
                    break;
                }
            }

            int num = 0;
            PixelFormat pixelFormat = m_imageSrc.PixelFormat;
            this.pgbSplit.Maximum = this.widthNumber * this.heightNumber;
            for (int j = 0; j < this.heightNumber; j++)
            {
                for (int i = 0; i < this.widthNumber; i++)
                {
                    Point point = new Point(i * this.widthPixel, j * this.heightPixel);
                    string text = string.Concat(new string[]
                    {
                        "",
                        j.ToString(),
                        "_",
                        i.ToString(),
                        this.saveFileFormat
                    });
                    this.lblMessage.Text = "正在分割图片... 保存为 [" + text + "]";
                    Bitmap bmp = CutImage(m_imageSrc, point.X, point.Y, this.widthPixel, this.heightPixel);
                    if (this.imageFormat == ImageFormat.Jpeg && jpegICIinfo != null)
                    {
                        bmp.Save(path + text, jpegICIinfo, ep);
                    }
                    else
                    {
                        bmp.Save(path + text, this.imageFormat);
                    }
                    //bitmap.Clone(new Rectangle(point.X, point.Y, this.widthPixel, this.heightPixel), pixelFormat).Save(text, this.imageFormat);
                    num++;
                    this.pgbSplit.Value = num;
                    Application.DoEvents();
                }
            }
            this.lblMessage.Text = "分割完毕";
            if (this.isCreateHtml)
            {
                string[] expr_151 = this.saveFileName.Split(new char[]
                {
                    '\\'
                });
                int upperBound = expr_151.GetUpperBound(0);
                string pictureName = expr_151[upperBound];
                this.CreateHTML(this.saveFileName, this.heightNumber, this.widthNumber, pictureName, this.saveFileFormat, false);
                this.CreateHTML(Application.StartupPath + "\\temp", this.heightNumber, this.widthNumber, this.saveFileName, this.saveFileFormat, true);
                Label expr_1B4 = this.lblMessage;
                expr_1B4.Text = expr_1B4.Text + ",相应HTML文档保存为 [" + this.saveFileName + ".htm]";
                this.chkOpenHTML.Visible = true;
            }
            //this.btnOK.Enabled = true;
            base.Close();
        }

        // Token: 0x0600004B RID: 75 RVA: 0x00004D92 File Offset: 0x00003D92
        private void timer1_Tick(object sender, EventArgs e)
        {
            this.timer1.Enabled = false;
            this.SplitPicture();
        }

        // Token: 0x0400003C RID: 60
        private string saveFileName;

        // Token: 0x0400003D RID: 61
        private string sourceFileName;

        // Token: 0x0400003E RID: 62
        private string saveFileFormat;

        // Token: 0x0400003F RID: 63
        private ImageFormat imageFormat;

        // Token: 0x04000040 RID: 64
        private int widthNumber = 0;

        // Token: 0x04000041 RID: 65
        private int heightNumber = 0;
        private int quality = 80;

        // Token: 0x04000042 RID: 66
        private int widthPixel = 0;

        // Token: 0x04000043 RID: 67
        private int heightPixel = 0;

        // Token: 0x04000044 RID: 68
        private bool isCreateHtml = false;
        private Bitmap m_imageSrc = null;
    }
}
